<?php
##############################################################################################
#	Compress URL (http://code.google.com/p/compressurl)
#	Author : Abhishek Verma (vermaabhishekp@gmail.com)
#
#	This file is part of Compress URL.
#	
#	Compress URL is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	Compress URL is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with Compress URL.  If not, see <http://www.gnu.org/licenses/>.
#	
#	compress_url.php : 
#
#		Main file that gets things started. Called from .htaccess file if an actual file
#		is not found by htttpd.
##############################################################################################

require_once("compress_url_libs/functions.inc.php");

// First things first
prevent_mysql_injection();

// GET variables posted, by say a bookmark like ?from=http://somelink.com/i_want_to_compress
$get = "";

if ( $MOD_REWRITE ) {
	// mod_rewrite is available. Get the requested uri
	$requested = empty ( $_SERVER['REQUEST_URI'] ) ? false : $_SERVER['REQUEST_URI'];
	
	if ( $DEBUG ) echo "Requesting : ".$requested;

	// split up the get vars in it by '?'
	$get_vars = explode("?", $requested);
	
	// make a temp copy
	$get_vars_temp = $get_vars;
	
	if ( $DEBUG ) print_r ( $get_vars_temp);
	
		// There are some get vars
	if (sizeof($get_vars_temp) > 1) {

		// Need to compress urls like http://site.com?name=value	
		// Remove first element
		array_shift($get_vars_temp);
		
		if ( $DEBUG ) print_r ( $get_vars_temp);
		
		//  And join the rest
		$get = implode("?", $get_vars_temp);
		if ( $DEBUG ) echo $get;
	}
	
	// The requested URI will contain the whole path from html root folder like /t/tinyurl
	
	// php_self will be /t/url_compressor.php
	$self = explode("/", $_SERVER['PHP_SELF']);
	
	if($DEBUG) echo "<br>Get var = ".$get;
	
	// take the first part of the url like /t/tinyurl and split it
	$url = explode ( "/", $get_vars[0] );
	
	if ( $DEBUG ) echo "Splitting url : ".print_r($url, true);
	
	// Compare each part of php_self with the shortened url
	for ( $i =0 ; $i < sizeof($self) -1 ; $i++) {
	
		if ( $DEBUG ) echo "<br/>Comparing : ".$url[$i]." == ".$self[$i];
		
		if($url[$i] != $self[$i]) {
			// Not sure if it will ever reach here...
			page_not_found();
		}
	
	}
	// this will give the last part
	$compressed_url = $url[$i];
}
else {

	// mod_rewrite is not available. Use get var of link like http://yourdomain.com/t/?q=tinyurl
	$compressed_url = $_GET['q'];
	
	// If the bookmark sent some "from" get var
	if(isset($_GET['from'])) 
		$get = $_GET['from'];
	else
		$get = "";
}

// The compressed_url is not "add", so check if there is an entry in table
if ( $compressed_url != $ADD_PATH ) {

	// check if the page exists & redirect to it
	// Optimisation : doing it earlier than add
	$result = query("select * from `".$MYSQL_TABLE."` where `to` = '".$compressed_url."'");
	if($a = mysql_fetch_array($result)) {
		// Found it. Redirecting...
		if ( $DEBUG ) echo "Redirecting to <a href='".$a['from']."'>".$a['from']."</a>";
		header("location: ".$a['from']);
	}
	else {
		// Could not find
		if ( $DEBUG ) echo "Cant find ".$compressed_url;
		page_not_found();
	}
}
else {
	// User is asking for /add , wants to add a new url
	include_once("compress_url_libs/add_url.php");
}
?>